package interview6;

import java.util.Stack;

/**
 * @author Jack
 * @date 2019-11-02-15:19
 * 面试题6:从尾到头打印链表(利用栈结构)
 */
public class Solution {

    public static void main(String[] args) {

        Node head = new Node(0);
        Node temp = head;
        for (int i = 0; i < 10; i++) {

            Node node = new Node(i+1);
            temp.setNext(node);
            temp = temp.getNext();

        }

        inverseLinkedList(head);

    }

    public static void inverseLinkedList(Node head){
        if(head == null){return ;}
        Stack<Node> stack = new Stack<>();
        Node temp = head;
        while(temp != null){
            stack.push(temp);
            temp = temp.getNext();
        }
        while(!stack.isEmpty()){
            temp = stack.pop();
            System.out.print(temp.getValue() + " ");
        }
    }

}

class Node{

    private int value;
    private Node next;

    public Node(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}